package 力扣日常刷题.木22二月.第02天207;

import 力扣日常刷题.ListNode;

/**
 * @author 帅小伙
 * @date 2022/2/7
 * @description
 */
public class Demo22合并两个排序的链表 {


    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode node = new ListNode(0);
        ListNode head = node;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                node.next = new ListNode(l1.val);
                l1 = l1.next;
            } else {
                node.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            node = node.next;
        }
        node.next = l1 !=null ? l1 : l2;
        return head.next;
    }

}
